﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Data;
using StoragePeriod.Common;
using System.Collections;
using StoragePeriod.Web.Common;
using System.Text;

namespace StoragePeriod.Web.SysManage
{
    public enum PrivType
    {
        Page = 0, Control = 1, Category = 2
    }

    public partial class PrivSysModule : SecurityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ddlModuleType.AutoPostBack = true;
            ddlModuleType.SelectedIndexChanged += new EventHandler(ddlModuleType_SelectedIndexChanged);

            if (!IsPostBack)
            {
                BindData();
            }
        }

        public void BindData()
        {
            this.AspNetPager1.RecordCount = LinqUtility.LinqContext.Privileges.Count();
            GridView1.DataSource = LinqUtility.LinqContext.Privileges.OrderBy(p => p.PrivType).Skip((this.AspNetPager1.CurrentPageIndex - 1) * this.AspNetPager1.PageSize).Take(this.AspNetPager1.PageSize).Select(p => new
            {
                模块名 = p.PrivName,
                模块分类 = LoadModuleType(p.PrivType),
                父级模块 = p.PrivParm1,//用于存放父级模块名
                模块描述 = p.PrivDesc,
                模块代码 = p.PrivCode
            });
            if (GridView1.DataSource != null)
                GridView1.DataBind();

            ddlParentNode.Items.AddRange(LinqUtility.LinqContext.Privileges.Where(p => p.PrivType == PrivType.Category.EToString())
                .Select(a => new ListItem() { Text = a.PrivName, Value = a.PrivCode }).ToArray());

            ddlPageNode.Items.Clear();
            ddlPageNode.Items.AddRange(LinqUtility.LinqContext.Privileges.Where(p => p.PrivType == PrivType.Page.EToString())
                .Select(a => new ListItem() { Text = a.PrivName, Value = a.PrivCode }).ToArray());
        }

        private string LoadModuleType(string p)
        {
            if (p.IsNullOrEmpty()) return string.Empty;
            try
            {

                switch (p)
                {
                    case "Page":
                        return "页面";
                    case "Control":
                        return "控件";
                    case "Category":
                        return "分类";
                }
            }
            catch (Exception)
            {

            }

            return "";

        }

        public void delete_Click(object sender, EventArgs e)
        {
            Button delete = sender as Button;
            string privCode = delete.CommandArgument;
            Privileges.Delete("PrivCode==@0", privCode);
            BindData();
        }

        void ddlModuleType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlModuleType.SelectedValue == "0")
            {
                PrivModuleView.Visible = false;
                SysModuleView.Visible = true;
            }
            else
            {
                PrivModuleView.Visible = true;
                SysModuleView.Visible = false;
            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            PrivType ParentPrivType = ddlParentNode.SelectedValue == "Root" ? PrivType.Category : PrivType.Page;
            string ParentCode = string.Empty, ParentName = string.Empty;

            if (ddlModuleType.SelectedValue == "1")
            {
                ParentPrivType = PrivType.Control;
                ParentCode = ddlPageNode.SelectedItem.Value;
                ParentName = ddlPageNode.SelectedItem.Text;
            }
            else
            {
                ParentCode = ddlParentNode.SelectedItem.Value;
                ParentName = ddlParentNode.SelectedItem.Text;
            }
            string PrivCode = Utility.NewPrivCode();
            if (!txtPrivCode.Text.IsNullOrEmpty())
            {
                PrivCode = txtPrivCode.Text;
            }

            Privileges newPriv = new Privileges()
            {
                PrivCode = PrivCode,
                PrivName = txtSysModuleName.Text.Trim(),
                PrivDesc = txtSysModuleDesc.Text.Trim(),
                PrivType = ParentPrivType.EToString(),

                PrivParm1 = ParentName,
                ParentPrivCode = ParentCode
            };
            Privileges.Insert(newPriv);

            BindData();
        }

        protected void GridView1_PreRender(object sender, EventArgs e)
        {
            if (GridView1.Rows.Count > 0)
            {
                // 使用<TH>替换<TD>
                GridView1.UseAccessibleHeader = true;

                //This will add the <thead> and <tbody> elements
                //HeaderRow将被<thead>包裹，数据行将被<tbody>包裹
                GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;

                // FooterRow将被<tfoot>包裹
                GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            //this.AspNetPager1.CurrentPageIndex += 1;
            this.BindData();
        }
    }
}